﻿using System;
using System.Collections.Generic;
using UnityEngine;
using ZZQFrameWork;

public class TaskManager:DDOLSingleton<TaskManager>
{
    public List<TaskData> taskDataList=new List<TaskData>();
    public List<TaskData> finishDataList=new List<TaskData>(); 

    public void InitTask()
    {
        TaskConfigTask[] tasks = TaskConfigProvider.Instance.GetAll();
        for (int i = 0; i < tasks.Length; i++)
        {
            taskDataList.Add(new TaskData(int.Parse(tasks[i].id)));
        }
        StartTask(taskDataList[0]);
    }

    //void OnEnable()
    //{
    //    EventDispatcher.AddListener<int>(Events.TaskEvent.FinishTaskToNpc, OnFinishTaskToNpc);
    //}
    //void OnDisable()
    //{
    //    EventDispatcher.RemoveListener<int>(Events.TaskEvent.FinishTaskToNpc, OnFinishTaskToNpc);
    //}

    void StartTask(TaskData data)
    {
        Debug.Log("开始任务，任务ID:"+data.id);
        //打开任务界面
        UIManager.Instance.OpenUI(Enum_UIOpenType.OpenSelf, Enum_UIType.TaskPanel);
        EventDispatcher.TriggerEvent(Events.TaskEvent.ShowTask, data);
    }

    public void OnFinishTask(TaskData data)
    {
        UIManager.Instance.OpenUI(Enum_UIOpenType.OpenSelf, Enum_UIType.TaskPanel);
        EventDispatcher.TriggerEvent(Events.TaskEvent.ShowTask, data);
    }

    public void OnFinishTaskToNpc(int taskId)
    {
        Debug.Log("Manager交任务！任务Id:"+taskId);
        TaskData data = taskDataList.Find((obj) =>
        {
            return obj.id == taskId;
        });
        taskDataList.Remove(data);
        finishDataList.Add(data);
        if (taskDataList.Count <= 0)
        {
            Debug.Log("任务全部完成，通关");
            //通关，打开通关界面
            UIManager.Instance.OpenUI(Enum_UIOpenType.OpenSelf, Enum_UIType.GamePassPanel);
        }
        else
        {
            //开启下一个任务
            StartTask(taskDataList[0]);
        }
    }
}
